/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import path from 'path';

import { expect } from '@jest/globals';
import fse from 'fs-extra';

import { getJson5Obj } from '../../../../src/utils/json-util';
import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1,
  },
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();

  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap',
  });
  const packageJsonPath = path.resolve(projectRoot, 'oh-package.json5');
  const packageJson = getJson5Obj(packageJsonPath);
  packageJson.devDependencies['@ohos/hypium'] = '1.0.19';
  fse.writeFileSync(packageJsonPath, JSON.stringify(packageJson));

  const result = await runHvigorAsync(projectRoot, commands);
  record.addStep('初次构建，检查是否构建成功', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  packageJson.devDependencies['@ohos/hypium'] = '1.0.18';
  fse.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
  const result1 = await runHvigorAsync(projectRoot, commands);
  record.addStep('修改devDependencies内容，再次构建', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :entry:default@CompileArkTS');

  const commandsHar = initExecuteCommand({
    mode: 'module',
    module: 'staticLibrary@default',
    product: 'default',
    buildMode: 'debug',
    tasks: 'assembleHar',
  });
  const harProfileJsonPath = path.resolve(project.getModule('staticLibrary').moduleRootPath(), 'build-profile.json5');
  const harProfileJson = getJson5Obj(harProfileJsonPath);
  harProfileJson.buildOption.arkOptions = {
    byteCodeHar: false,
  };
  await fse.writeFile(harProfileJsonPath, JSON.stringify(harProfileJson));
  const result2 = await runHvigorAsync(projectRoot, commandsHar);
  record.addStep('初次构建har，检查是否构建成功', result2);
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  packageJson.devDependencies['@ohos/hypium'] = '1.0.19';
  fse.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
  const result3 = await runHvigorAsync(projectRoot, commandsHar);
  record.addStep('修改devDependencies内容，再次构建har', result3);
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('DTS2024101820297')
  // eslint-disable-next-line max-len
  .description('修改项目级oh-package.json5文件中的devDependencies内容，重新编译')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
